package com.yricky.atri.viewmodel

import com.yricky.atri.model.MLModelManager
import com.yricky.atri.model.pojo.ModelMetaData
import com.yricky.mvi.StateViewModel
import kotlinx.coroutines.Dispatchers
import java.io.File
import java.io.InputStream

/**
 * @author Yricky
 * @date 2021/12/8
 */
class MainActivityViewModel: StateViewModel<MainActivityViewModel.ViewState>() {

    data class ViewState(
        val modelList:List<Pair<File,ModelMetaData>> = emptyList()
    )
    override val initState = { ViewState() }


    fun listModels() = updateState(Dispatchers.IO){
        it.copy(modelList = MLModelManager.getModels())
    }

    fun addModel(name:String,ins:InputStream){
        updateState(Dispatchers.IO){
            MLModelManager.importModel(name,ins)
            it.copy(modelList = MLModelManager.getModels())
        }
    }
}